<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>calculator.cpp 示例文件 | Qt Widgets 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtwidgets-index.html">Qt Widgets</a></td><td ><a href="qtwidgets-widgets-calculator-example.html">Calculator Example</a></td><td >calculator.cpp 示例文件</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtwidgets-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar"><div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">calculator.cpp 示例文件</h1>
<span class="subtitle">widgets/calculator/calculator.cpp</span>
<!-- $$$widgets/calculator/calculator.cpp-description -->
<div class="descr"> <a name="details"></a>
<pre class="cpp">

  <span class="comment">/****************************************************************************
  **
  ** Copyright (C) 2016 The Qt Company Ltd.
  ** Contact: https://www.qt.io/licensing/
  **
  ** This file is part of the examples of the Qt Toolkit.
  **
  ** $QT_BEGIN_LICENSE:BSD$
  ** Commercial License Usage
  ** Licensees holding valid commercial Qt licenses may use this file in
  ** accordance with the commercial license agreement provided with the
  ** Software or, alternatively, in accordance with the terms contained in
  ** a written agreement between you and The Qt Company. For licensing terms
  ** and conditions see https://www.qt.io/terms-conditions. For further
  ** information use the contact form at https://www.qt.io/contact-us.
  **
  ** BSD License Usage
  ** Alternatively, you may use this file under the terms of the BSD license
  ** as follows:
  **
  ** &quot;Redistribution and use in source and binary forms, with or without
  ** modification, are permitted provided that the following conditions are
  ** met:
  **   * Redistributions of source code must retain the above copyright
  **     notice, this list of conditions and the following disclaimer.
  **   * Redistributions in binary form must reproduce the above copyright
  **     notice, this list of conditions and the following disclaimer in
  **     the documentation and/or other materials provided with the
  **     distribution.
  **   * Neither the name of The Qt Company Ltd nor the names of its
  **     contributors may be used to endorse or promote products derived
  **     from this software without specific prior written permission.
  **
  **
  ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  ** &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&quot;
  **
  ** $QT_END_LICENSE$
  **
  ****************************************************************************/</span>

  <span class="preprocessor">#include &lt;QtWidgets&gt;</span>

  <span class="preprocessor">#include &lt;cmath&gt;</span>

  <span class="preprocessor">#include &quot;button.h&quot;</span>
  <span class="preprocessor">#include &quot;calculator.h&quot;</span>

  Calculator<span class="operator">::</span>Calculator(<span class="type"><a href="qwidget.html">QWidget</a></span> <span class="operator">*</span>parent)
      : <span class="type"><a href="qwidget.html">QWidget</a></span>(parent)
  {
      sumInMemory <span class="operator">=</span> <span class="number">0.0</span>;
      sumSoFar <span class="operator">=</span> <span class="number">0.0</span>;
      factorSoFar <span class="operator">=</span> <span class="number">0.0</span>;
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;

      display <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qlineedit.html">QLineEdit</a></span>(<span class="string">&quot;0&quot;</span>);
      display<span class="operator">-</span><span class="operator">&gt;</span>setReadOnly(<span class="keyword">true</span>);
      display<span class="operator">-</span><span class="operator">&gt;</span>setAlignment(<span class="type"><a href="../qtcore/qt.html">Qt</a></span><span class="operator">::</span>AlignRight);
      display<span class="operator">-</span><span class="operator">&gt;</span>setMaxLength(<span class="number">15</span>);

      <span class="type"><a href="../qtgui/qfont.html">QFont</a></span> font <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>font();
      font<span class="operator">.</span>setPointSize(font<span class="operator">.</span>pointSize() <span class="operator">+</span> <span class="number">8</span>);
      display<span class="operator">-</span><span class="operator">&gt;</span>setFont(font);

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">0</span>; i <span class="operator">&lt;</span> NumDigitButtons; <span class="operator">+</span><span class="operator">+</span>i) {
          digitButtons<span class="operator">[</span>i<span class="operator">]</span> <span class="operator">=</span> createButton(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(i)<span class="operator">,</span> SLOT(digitClicked()));
      }

      Button <span class="operator">*</span>pointButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;.&quot;</span>)<span class="operator">,</span> SLOT(pointClicked()));
      Button <span class="operator">*</span>changeSignButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;\302\261&quot;</span>)<span class="operator">,</span> SLOT(changeSignClicked()));

      Button <span class="operator">*</span>backspaceButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;Backspace&quot;</span>)<span class="operator">,</span> SLOT(backspaceClicked()));
      Button <span class="operator">*</span>clearButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;Clear&quot;</span>)<span class="operator">,</span> SLOT(clear()));
      Button <span class="operator">*</span>clearAllButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;Clear All&quot;</span>)<span class="operator">,</span> SLOT(clearAll()));

      Button <span class="operator">*</span>clearMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;MC&quot;</span>)<span class="operator">,</span> SLOT(clearMemory()));
      Button <span class="operator">*</span>readMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;MR&quot;</span>)<span class="operator">,</span> SLOT(readMemory()));
      Button <span class="operator">*</span>setMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;MS&quot;</span>)<span class="operator">,</span> SLOT(setMemory()));
      Button <span class="operator">*</span>addToMemoryButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;M+&quot;</span>)<span class="operator">,</span> SLOT(addToMemory()));

      Button <span class="operator">*</span>divisionButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;\303\267&quot;</span>)<span class="operator">,</span> SLOT(multiplicativeOperatorClicked()));
      Button <span class="operator">*</span>timesButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;\303\227&quot;</span>)<span class="operator">,</span> SLOT(multiplicativeOperatorClicked()));
      Button <span class="operator">*</span>minusButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;-&quot;</span>)<span class="operator">,</span> SLOT(additiveOperatorClicked()));
      Button <span class="operator">*</span>plusButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;+&quot;</span>)<span class="operator">,</span> SLOT(additiveOperatorClicked()));

      Button <span class="operator">*</span>squareRootButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;Sqrt&quot;</span>)<span class="operator">,</span> SLOT(unaryOperatorClicked()));
      Button <span class="operator">*</span>powerButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;x\302\262&quot;</span>)<span class="operator">,</span> SLOT(unaryOperatorClicked()));
      Button <span class="operator">*</span>reciprocalButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;1/x&quot;</span>)<span class="operator">,</span> SLOT(unaryOperatorClicked()));
      Button <span class="operator">*</span>equalButton <span class="operator">=</span> createButton(tr(<span class="string">&quot;=&quot;</span>)<span class="operator">,</span> SLOT(equalClicked()));

      <span class="type"><a href="qgridlayout.html">QGridLayout</a></span> <span class="operator">*</span>mainLayout <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qgridlayout.html">QGridLayout</a></span>;
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>setSizeConstraint(<span class="type"><a href="qlayout.html">QLayout</a></span><span class="operator">::</span>SetFixedSize);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(display<span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">6</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(backspaceButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">0</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(clearButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(clearAllButton<span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">1</span><span class="operator">,</span> <span class="number">2</span>);

      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(clearMemoryButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(readMemoryButton<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(setMemoryButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">0</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(addToMemoryButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">0</span>);

      <span class="keyword">for</span> (<span class="type">int</span> i <span class="operator">=</span> <span class="number">1</span>; i <span class="operator">&lt;</span> NumDigitButtons; <span class="operator">+</span><span class="operator">+</span>i) {
          <span class="type">int</span> row <span class="operator">=</span> ((<span class="number">9</span> <span class="operator">-</span> i) <span class="operator">/</span> <span class="number">3</span>) <span class="operator">+</span> <span class="number">2</span>;
          <span class="type">int</span> column <span class="operator">=</span> ((i <span class="operator">-</span> <span class="number">1</span>) <span class="operator">%</span> <span class="number">3</span>) <span class="operator">+</span> <span class="number">1</span>;
          mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(digitButtons<span class="operator">[</span>i<span class="operator">]</span><span class="operator">,</span> row<span class="operator">,</span> column);
      }

      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(digitButtons<span class="operator">[</span><span class="number">0</span><span class="operator">]</span><span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">1</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(pointButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">2</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(changeSignButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">3</span>);

      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(divisionButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">4</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(timesButton<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">4</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(minusButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">4</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(plusButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">4</span>);

      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(squareRootButton<span class="operator">,</span> <span class="number">2</span><span class="operator">,</span> <span class="number">5</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(powerButton<span class="operator">,</span> <span class="number">3</span><span class="operator">,</span> <span class="number">5</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(reciprocalButton<span class="operator">,</span> <span class="number">4</span><span class="operator">,</span> <span class="number">5</span>);
      mainLayout<span class="operator">-</span><span class="operator">&gt;</span>addWidget(equalButton<span class="operator">,</span> <span class="number">5</span><span class="operator">,</span> <span class="number">5</span>);
      setLayout(mainLayout);

      setWindowTitle(tr(<span class="string">&quot;Calculator&quot;</span>));
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>digitClicked()
  {
      Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>Button <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="type">int</span> digitValue <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toInt();
      <span class="keyword">if</span> (display<span class="operator">-</span><span class="operator">&gt;</span>text() <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;0&quot;</span> <span class="operator">&amp;</span><span class="operator">&amp;</span> digitValue <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>)
          <span class="keyword">return</span>;

      <span class="keyword">if</span> (waitingForOperand) {
          display<span class="operator">-</span><span class="operator">&gt;</span>clear();
          waitingForOperand <span class="operator">=</span> <span class="keyword">false</span>;
      }
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(display<span class="operator">-</span><span class="operator">&gt;</span>text() <span class="operator">+</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(digitValue));
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>unaryOperatorClicked()
  {
      Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>Button <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> clickedOperator <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">&gt;</span>text();
      <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toDouble();
      <span class="type">double</span> result <span class="operator">=</span> <span class="number">0.0</span>;

      <span class="keyword">if</span> (clickedOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;Sqrt&quot;</span>)) {
          <span class="keyword">if</span> (operand <span class="operator">&lt;</span> <span class="number">0.0</span>) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          result <span class="operator">=</span> std<span class="operator">::</span>sqrt(operand);
      } <span class="keyword">else</span> <span class="keyword">if</span> (clickedOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;x\302\262&quot;</span>)) {
          result <span class="operator">=</span> std<span class="operator">::</span>pow(operand<span class="operator">,</span> <span class="number">2.0</span>);
      } <span class="keyword">else</span> <span class="keyword">if</span> (clickedOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;1/x&quot;</span>)) {
          <span class="keyword">if</span> (operand <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          result <span class="operator">=</span> <span class="number">1.0</span> <span class="operator">/</span> operand;
      }
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(result));
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>additiveOperatorClicked()
  {
      Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>Button <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> clickedOperator <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">&gt;</span>text();
      <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toDouble();

      <span class="keyword">if</span> (<span class="operator">!</span>pendingMultiplicativeOperator<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingMultiplicativeOperator)) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(factorSoFar));
          operand <span class="operator">=</span> factorSoFar;
          factorSoFar <span class="operator">=</span> <span class="number">0.0</span>;
          pendingMultiplicativeOperator<span class="operator">.</span>clear();
      }

      <span class="keyword">if</span> (<span class="operator">!</span>pendingAdditiveOperator<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingAdditiveOperator)) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(sumSoFar));
      } <span class="keyword">else</span> {
          sumSoFar <span class="operator">=</span> operand;
      }

      pendingAdditiveOperator <span class="operator">=</span> clickedOperator;
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>multiplicativeOperatorClicked()
  {
      Button <span class="operator">*</span>clickedButton <span class="operator">=</span> qobject_cast<span class="operator">&lt;</span>Button <span class="operator">*</span><span class="operator">&gt;</span>(sender());
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> clickedOperator <span class="operator">=</span> clickedButton<span class="operator">-</span><span class="operator">&gt;</span>text();
      <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toDouble();

      <span class="keyword">if</span> (<span class="operator">!</span>pendingMultiplicativeOperator<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingMultiplicativeOperator)) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(factorSoFar));
      } <span class="keyword">else</span> {
          factorSoFar <span class="operator">=</span> operand;
      }

      pendingMultiplicativeOperator <span class="operator">=</span> clickedOperator;
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>equalClicked()
  {
      <span class="type">double</span> operand <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toDouble();

      <span class="keyword">if</span> (<span class="operator">!</span>pendingMultiplicativeOperator<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingMultiplicativeOperator)) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          operand <span class="operator">=</span> factorSoFar;
          factorSoFar <span class="operator">=</span> <span class="number">0.0</span>;
          pendingMultiplicativeOperator<span class="operator">.</span>clear();
      }
      <span class="keyword">if</span> (<span class="operator">!</span>pendingAdditiveOperator<span class="operator">.</span>isEmpty()) {
          <span class="keyword">if</span> (<span class="operator">!</span>calculate(operand<span class="operator">,</span> pendingAdditiveOperator)) {
              abortOperation();
              <span class="keyword">return</span>;
          }
          pendingAdditiveOperator<span class="operator">.</span>clear();
      } <span class="keyword">else</span> {
          sumSoFar <span class="operator">=</span> operand;
      }

      display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(sumSoFar));
      sumSoFar <span class="operator">=</span> <span class="number">0.0</span>;
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>pointClicked()
  {
      <span class="keyword">if</span> (waitingForOperand)
          display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;0&quot;</span>);
      <span class="keyword">if</span> (<span class="operator">!</span>display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>contains(<span class="char">'.'</span>))
          display<span class="operator">-</span><span class="operator">&gt;</span>setText(display<span class="operator">-</span><span class="operator">&gt;</span>text() <span class="operator">+</span> tr(<span class="string">&quot;.&quot;</span>));
      waitingForOperand <span class="operator">=</span> <span class="keyword">false</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>changeSignClicked()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text();
      <span class="type">double</span> value <span class="operator">=</span> text<span class="operator">.</span>toDouble();

      <span class="keyword">if</span> (value <span class="operator">&gt;</span> <span class="number">0.0</span>) {
          text<span class="operator">.</span>prepend(tr(<span class="string">&quot;-&quot;</span>));
      } <span class="keyword">else</span> <span class="keyword">if</span> (value <span class="operator">&lt;</span> <span class="number">0.0</span>) {
          text<span class="operator">.</span>remove(<span class="number">0</span><span class="operator">,</span> <span class="number">1</span>);
      }
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(text);
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>backspaceClicked()
  {
      <span class="keyword">if</span> (waitingForOperand)
          <span class="keyword">return</span>;

      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> text <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text();
      text<span class="operator">.</span>chop(<span class="number">1</span>);
      <span class="keyword">if</span> (text<span class="operator">.</span>isEmpty()) {
          text <span class="operator">=</span> <span class="string">&quot;0&quot;</span>;
          waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
      }
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(text);
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>clear()
  {
      <span class="keyword">if</span> (waitingForOperand)
          <span class="keyword">return</span>;

      display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;0&quot;</span>);
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>clearAll()
  {
      sumSoFar <span class="operator">=</span> <span class="number">0.0</span>;
      factorSoFar <span class="operator">=</span> <span class="number">0.0</span>;
      pendingAdditiveOperator<span class="operator">.</span>clear();
      pendingMultiplicativeOperator<span class="operator">.</span>clear();
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="string">&quot;0&quot;</span>);
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>clearMemory()
  {
      sumInMemory <span class="operator">=</span> <span class="number">0.0</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>readMemory()
  {
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(<span class="type"><a href="../qtcore/qstring.html">QString</a></span><span class="operator">::</span>number(sumInMemory));
      waitingForOperand <span class="operator">=</span> <span class="keyword">true</span>;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>setMemory()
  {
      equalClicked();
      sumInMemory <span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toDouble();
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>addToMemory()
  {
      equalClicked();
      sumInMemory <span class="operator">+</span><span class="operator">=</span> display<span class="operator">-</span><span class="operator">&gt;</span>text()<span class="operator">.</span>toDouble();
  }
  Button <span class="operator">*</span>Calculator<span class="operator">::</span>createButton(<span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>text<span class="operator">,</span> <span class="keyword">const</span> <span class="type">char</span> <span class="operator">*</span>member)
  {
      Button <span class="operator">*</span>button <span class="operator">=</span> <span class="keyword">new</span> Button(text);
      connect(button<span class="operator">,</span> SIGNAL(clicked())<span class="operator">,</span> <span class="keyword">this</span><span class="operator">,</span> member);
      <span class="keyword">return</span> button;
  }

  <span class="type">void</span> Calculator<span class="operator">::</span>abortOperation()
  {
      clearAll();
      display<span class="operator">-</span><span class="operator">&gt;</span>setText(tr(<span class="string">&quot;####&quot;</span>));
  }

  bool Calculator<span class="operator">::</span>calculate(<span class="type">double</span> rightOperand<span class="operator">,</span> <span class="keyword">const</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span> <span class="operator">&amp;</span>pendingOperator)
  {
      <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;+&quot;</span>)) {
          sumSoFar <span class="operator">+</span><span class="operator">=</span> rightOperand;
      } <span class="keyword">else</span> <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;-&quot;</span>)) {
          sumSoFar <span class="operator">-</span><span class="operator">=</span> rightOperand;
      } <span class="keyword">else</span> <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;\303\227&quot;</span>)) {
          factorSoFar <span class="operator">*</span><span class="operator">=</span> rightOperand;
      } <span class="keyword">else</span> <span class="keyword">if</span> (pendingOperator <span class="operator">=</span><span class="operator">=</span> tr(<span class="string">&quot;\303\267&quot;</span>)) {
          <span class="keyword">if</span> (rightOperand <span class="operator">=</span><span class="operator">=</span> <span class="number">0.0</span>)
              <span class="keyword">return</span> <span class="keyword">false</span>;
          factorSoFar <span class="operator">/</span><span class="operator">=</span> rightOperand;
      }
      <span class="keyword">return</span> <span class="keyword">true</span>;
  }

</pre>
</div>
<!-- @@@widgets/calculator/calculator.cpp -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
